//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Ranlux4UniformRng.h"
using namespace Cephei::QL::Math::Randomnumbers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (Microsoft::FSharp::Core::FSharpOption<UInt64>^ seed) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phRanlux4UniformRng = NULL;
#endif
        QuantLib::Size _seed = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (seed) ? (QuantLib::Size)ValueHelper::Convert (seed->Value) : 19780503u); //4
        _ppRanlux4UniformRng = new boost::shared_ptr<QuantLib::Ranlux4UniformRng> (new QuantLib::Ranlux4UniformRng ( _seed ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (boost::shared_ptr<QuantLib::Ranlux4UniformRng>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRanlux4UniformRng = NULL;
#endif
	_ppRanlux4UniformRng = &childNative;
    
}
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (QuantLib::Ranlux4UniformRng& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phRanlux4UniformRng = NULL;
#endif
	_ppRanlux4UniformRng = new boost::shared_ptr<QuantLib::Ranlux4UniformRng> (&childNative);
    
    _Ranlux4UniformRngOwner = owner;
    
}

Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (CRanlux4UniformRng^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRanlux4UniformRng = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRanlux4UniformRng = new boost::shared_ptr<QuantLib::Ranlux4UniformRng> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRanlux4UniformRng = NULL;
#endif
	if (!t->IsSubclassOf(CRanlux4UniformRng::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (QuantLib::Handle<QuantLib::Ranlux4UniformRng>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRanlux4UniformRng = &childNative;
	_ppRanlux4UniformRng = &static_cast<boost::shared_ptr<QuantLib::Ranlux4UniformRng>>(childNative.currentLink());
    
    _Ranlux4UniformRngOwner = owner;
}
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (QuantLib::Handle<QuantLib::Ranlux4UniformRng> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRanlux4UniformRng = &childNative;
	_ppRanlux4UniformRng = &static_cast<boost::shared_ptr<QuantLib::Ranlux4UniformRng>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::CRanlux4UniformRng (QuantLib::Ranlux4UniformRng childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRanlux4UniformRng = NULL;
#endif
	_ppRanlux4UniformRng = new boost::shared_ptr<QuantLib::Ranlux4UniformRng> (new QuantLib::Ranlux4UniformRng (childNative));
    
}
#endif

Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::~CRanlux4UniformRng ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppRanlux4UniformRng != NULL)
    {
	    delete _ppRanlux4UniformRng;
        _ppRanlux4UniformRng = NULL;
    }
}
Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::!CRanlux4UniformRng ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppRanlux4UniformRng != NULL)
    {
	    delete _ppRanlux4UniformRng;
    }
}
QuantLib::Ranlux4UniformRng& Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::GetReference ()
{
    if (_ppRanlux4UniformRng == NULL) throw REFNEW NativeNullException ();
	return **_ppRanlux4UniformRng;
}
boost::shared_ptr<QuantLib::Ranlux4UniformRng>& Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::GetShared ()
{
    if (_ppRanlux4UniformRng == NULL) throw REFNEW NativeNullException ();
	return *_ppRanlux4UniformRng;
}
QuantLib::Ranlux4UniformRng* Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::GetPointer ()
{
    if (_ppRanlux4UniformRng == NULL) throw REFNEW NativeNullException ();
	return &**_ppRanlux4UniformRng;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Ranlux4UniformRng>& Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::GetHandle ()
{
	if (_phRanlux4UniformRng == NULL)
	{
		_phRanlux4UniformRng = new Handle<QuantLib::Ranlux4UniformRng> (*_ppRanlux4UniformRng);
	}
	return *_phRanlux4UniformRng;
}
#endif
bool Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng::HasNative () 
{
	return (_ppRanlux4UniformRng != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Randomnumbers::IRanlux4UniformRng^ Cephei::QL::Math::Randomnumbers::CRanlux4UniformRng_Factory::Create (Microsoft::FSharp::Core::FSharpOption<UInt64>^ seed)
{
    return REFNEW CRanlux4UniformRng ( seed);
}
